package cn.xyafu.lost.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import cn.xyafu.lost.R;
import cn.xyafu.lost.pojo.News;

public class NewsBodyAdapter extends BaseAdapter {

    private final Context context;  // 上下文
    private final List<News> news; //数据源

    public NewsBodyAdapter(Context context, List<News> news) {
        this.context = context;
        this.news = news;
    }

    @Override
    public int getCount() {
        return news==null?0:news.size();
    }

    @Override
    public Object getItem(int position) {
        return news.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        News news1 = (News) getItem(position);
        NewsBodyAdapter.ViewHolder viewHolder = null;
        if(convertView == null){ // 没有加载过,那么就创建
            viewHolder = new NewsBodyAdapter.ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.news_item_layout,parent,false);
            viewHolder.news_send_name =  convertView.findViewById(R.id.news_send_name);
            viewHolder.news_send_uid =  convertView.findViewById(R.id.news_send_uid);
            viewHolder.news_send_content = convertView.findViewById(R.id.news_send_content);
            viewHolder.news_send_time = convertView.findViewById(R.id.news_send_time);
            viewHolder.news_send_nid = convertView.findViewById(R.id.news_send_nid);
            convertView.setTag(viewHolder);

        }else{//加载过,那么就获取之前的
            viewHolder = (NewsBodyAdapter.ViewHolder) convertView.getTag();
        }
        // (3). 给组件添加数据
        viewHolder.news_send_name.setText(news1.getSend().getUnikename());
        viewHolder.news_send_uid.setText(String.valueOf(news1.getSend().getUid()));
        viewHolder.news_send_content.setText(news1.getNmsg());
        viewHolder.news_send_time.setText(news1.getNdate());
        viewHolder.news_send_nid.setText(String.valueOf(news1.getNid()));

        return convertView;

    }

    class ViewHolder {

        TextView news_send_name;
        TextView news_send_uid;
        TextView news_send_content;
        TextView news_send_time;

        TextView news_send_nid;

    }
}
